﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Googler.DAL.BLL
{
    public class DBConverter
    {
        public static DateTime ToDateTime(object obj)
        {
            if (obj == DBNull.Value)
                return DateTime.MinValue;
            else
                return Convert.ToDateTime(obj);
        }

        public static TimeSpan ToTimeSpane(object obj)
        {
            if (obj == DBNull.Value)
                return TimeSpan.MinValue;
            else
                return new TimeSpan(Convert.ToInt64(obj));
        }
        
        public static byte ToByte(object obj)
        {
            if (obj == DBNull.Value)
                return 0;
            else
                return Convert.ToByte(obj);
        }
        public static Int32 ToInt32(object obj)
        {
            if (obj == DBNull.Value)
                return 0;
            else
                return Convert.ToInt32(obj);
        }
        public static long ToInt64(object obj)
        {
            if (obj == DBNull.Value)
                return 0;
            else
                return Convert.ToInt64(obj);
        }
        public static bool ToBoolean(object obj)
        {
            if (obj == DBNull.Value)
                return false;
            else
                return Convert.ToBoolean(obj);
        }
        public static String ToString(object obj)
        {
            if (obj == DBNull.Value)
                return string.Empty;
            else
                return Convert.ToString(obj);
        }
        public static Decimal ToDecimal(object obj)
        {
            if (obj == DBNull.Value)
                return 0;
            else
                return Convert.ToDecimal(obj);
        }


        public static object FromDateTime(DateTime date)
        {
            return date == DateTime.MinValue?(object) DBNull.Value:(object)date;
        }

        
        public static string ToString(object value, string valueIfNull)
        {
            string valueString = ToString(value);
            valueString = string.IsNullOrEmpty(valueString) ? valueIfNull : valueString;

            return valueString;
        }

        public static object FromString(string value)
        {
            return string.IsNullOrEmpty(value) ? (object)DBNull.Value : (object)value;
        }


        
    }
}
